package com.mitu.nio;

import java.nio.ByteBuffer;

public class NIOBufferReadOnly {
    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(64);
        for (int i = 0; i < 64; i++) {
            buffer.put((byte) i);
        }

        buffer.flip();
        //得到一个只读Buffer
        ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
        System.out.println(buffer.isReadOnly());
        System.out.println(readOnlyBuffer.isReadOnly());
        System.out.println(readOnlyBuffer.getClass());

        //读取
        while (readOnlyBuffer.hasRemaining()){
            System.out.println(readOnlyBuffer.get());
        }

        //存放数据会抛出异常ReadOnlyBufferException
        readOnlyBuffer.put((byte)12);
    }
}
